package tingshu.servlet.album;

import lombok.SneakyThrows;
import tingshu.util.DBUtil;
import tingshu.util.Log;
import tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * 取消音频关联
 * @author houdaiqin
 * @date 2022/07/20 15:12
 **/
@WebServlet("/studio/album/unbind.do")
public class UnBindJsonServlet extends HttpServlet {
    @Override
    @SneakyThrows
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserVO currentUser=null;
        HttpSession session=req.getSession(false);
        if (session!=null){
            currentUser=(UserVO) session.getAttribute("currentUser");
        }else{
            Log.println("用户未登录");
            return;
        }
        if (currentUser==null){
            Log.println("用户未登录");
            return;
        }
        int tid=Integer.parseInt(req.getParameter("tid"));
        int aid=Integer.parseInt(req.getParameter("aid"));

        try(Connection c= DBUtil.connection()) {
            String sql = "delete from relations where aid=? and tid=?";
            Log.println("执行SQL"+sql);
            try(PreparedStatement ps=c.prepareStatement(sql)){
                ps.setInt(1,aid);
                ps.setInt(2,tid);
                ps.executeUpdate();
            }
        }

        resp.sendRedirect("/studio/album/bind.html?aid="+aid);
//        resp.setCharacterEncoding("utf-8");
//        resp.setContentType("text/plain");
//        resp.getWriter().println("取消关联成功");
    }
}